package news.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import news.model.*;
import news.service.FrontUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "前台用户管理")
@RestController
@RequestMapping("/userApi/userCtl")
public class FrontUserController {

    @Autowired
    private FrontUserService frontUserService;


    /**
     * 生成验证码
     * @param email
     * @return
     */
    @ApiOperation(value = "生成验证码")
    @GetMapping("/createYzm")
    public CommonResult createYzm(String email){
        int res = frontUserService.createYzm(email);
        return new CommonResult(200,res,"生成邮箱成功");
    }

    /**
     * 用户注册保存
     * @return
     */
    @ApiOperation("前台用户注册保存")
    @PostMapping("/save")
    public CommonResult createYzm(@RequestBody User user){
        int res = frontUserService.save(user);
        return new CommonResult(200,res,"保存成功");
    }

    /**
     * 前台用户登录
     * @param user
     * @return
     */
    @ApiOperation(value = "前台用户登录")
    @PostMapping("/userLogin")
    public CommonResult userLogin(@RequestBody User user){
        CommonResult commonResult = frontUserService.userLogin(user);
        return commonResult;
    }

    @ApiOperation(value = "前台用户保存评论")
    @GetMapping("/saveComment/{newsid}/{comment}")
    public CommonResult saveComment(@PathVariable("newsid") Integer newsid,
                                    @PathVariable String comment,
                                    @RequestHeader("userToken") String userToken){
       frontUserService.saveComment(newsid,comment,userToken);
       return new CommonResult(200,"保存评论成功");
    }

    /**
     * 判断用户是否点赞
     * @param
     * @return
     */
    @ApiOperation(value = "查询用户是否点赞")
    @GetMapping("/checkUserIsDZ")
    public CommonResult checkUserIsDZ(String newsid, @RequestHeader("userToken") String userToken){
        Boolean res = frontUserService.checkUserIsDZ(newsid,userToken);
        return new CommonResult(200,res,"查询点赞成功");
    }

    @ApiOperation(value = "用户点赞")
    @GetMapping("/dz")
    public CommonResult dz(String newsid, @RequestHeader("userToken") String userToken,Boolean isDZ){
        frontUserService.dz(newsid,userToken,isDZ);
        return new CommonResult(200,"点赞");
    }

    @ApiOperation(value = "查询用户是否收藏")
    @GetMapping("/checkUserIsSC")
    public CommonResult checkUserIsSC(String newsid, @RequestHeader("userToken") String userToken){
        Boolean res = frontUserService.checkUserIsSC(newsid,userToken);
        return new CommonResult(200,res,"查询收藏成功");
    }
    @ApiOperation(value = "用户收藏")
    @GetMapping("/sc")
    public CommonResult sc(Integer newsid, @RequestHeader("userToken") String userToken,Boolean isSC,String title){
        frontUserService.sc(newsid,userToken,isSC,title);
        return new CommonResult(200,"收藏");
    }

    /**
     * 更改用户信息
     * @param
     * @param userToken
     * @return
     */
    @ApiOperation(value = "保存用户信息")
    @PostMapping("/saveInfo")
    public CommonResult saveInfo(String nickname,String password,
            String phone,String gender,@RequestHeader("userToken") String userToken){
        frontUserService.saveInfo(nickname,password,phone,gender,userToken);
        return new CommonResult(200,"更改用户信息成功");
    }

    @ApiOperation(value = "获取前台用户信息")
    @GetMapping("/getUserInfo")
    public CommonResult getUserInfo(@RequestHeader("userToken") String userToken){
        User user= frontUserService.getUserInfo(userToken);
        return new CommonResult(200,user,"获取前台用户信息");
    }

    /**
     * 获取用户收藏
     * @param userToken
     * @return
     */
    @ApiOperation(value = "获取用户收藏列表")
    @GetMapping("/collectionNews")
    public CommonResult collectionNews(@RequestHeader("userToken") String userToken){
        List<News> res = frontUserService.collectionNews(userToken);
        return new CommonResult(200,res,"获得用户收藏列表成功");
    }

    /**
     * 获得用户的评论列表
     * @param userToken
     * @return
     */
    @ApiOperation(value = "获取用户评论列表")
    @GetMapping("/commentNews")
    public CommonResult commentNews(@RequestHeader("userToken") String userToken){
        List<List<News>> res = frontUserService.commentNews(userToken);
        return new CommonResult(200,res,"获得用户评论列表成功");
    }
}
